<?php

declare(strict_types=1);

namespace App\Request\Web;

use Hyperf\Validation\Request\FormRequest;

class RegisterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }
    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'name' => [
                'required',
                'string',
                'min:2',
                'max:20',
                'regex:/^[\x{4e00}-\x{9fa5}a-zA-Z0-9_]+$/u',
                'unique:users,name'
            ],
            'email' => [
                'required',
                'email:rfc,dns',
                'max:100',
                'unique:users,email'
            ],
            'password' => [
                'required',
                'string',
                'min:8',
                'confirmed',
                'regex:/^(?=.*[A-Za-z])(?=.*\d).+$/'
            ],
            'password_confirmation' => 'required|string|min:8'
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => '用户名不能为空',
            'name.min' => '用户名至少2个字符',
            'name.max' => '用户名不能超过20个字符',
            'name.regex' => '用户名只能包含中文、英文、数字和下划线',
            'name.unique' => '用户名已被注册',

            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
            'email.max' => '邮箱不能超过100个字符',
            'email.unique' => '邮箱已被注册',

            'password.required' => '密码不能为空',
            'password.min' => '密码至少8个字符',
            'password.confirmed' => '两次输入的密码不一致',
            'password.regex' => '密码必须包含字母和数字',

            'password_confirmation.required' => '确认密码不能为空',
        ];
    }

    public function attributes(): array
    {
        return [
            'name' => '用户名',
            'email' => '邮箱',
            'password' => '密码',
            'password_confirmation' => '确认密码',
        ];
    }
}
